#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>

#define PLUGIN_NAME		"Infected Ghost Pounce"
#define PLUGIN_DESC		"Allows infected make high jump while ghost"
#define PLUGIN_VERSION		"1.0.5"
#define PLUGIN_FILENAME		"l4d2_ghostpounce"

ConVar g_hVectorMultX; 
ConVar g_hVectorMultZ; 
ConVar g_hVectorMultY;

int g_iVectorMultX;
int g_iVectorMultZ;

float g_fVectorMultY;

public Plugin myinfo = 
{
  name = PLUGIN_NAME,
  author = " AtomicStryker",
  description = PLUGIN_DESC,
  version = PLUGIN_VERSION,
  url = "http://forums.alliedmods.net/showthread.php?t=99519"
};

public void OnPluginStart()
{
	/* Load ConVar */
	CreateConVar("l4d_ghostpounce_version", PLUGIN_VERSION, " Ghost Leap Plugin Version ", FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_DONTRECORD);
	g_hVectorMultX = CreateConVar("l4d_ghostpounce_vector_mult_x", "3", "Multiplication speed when pounce on x", FCVAR_NOTIFY); 
	g_hVectorMultZ = CreateConVar("l4d_ghostpounce_vector_mult_z", "3", "Multiplication speed when pounce on z", FCVAR_NOTIFY); 
	g_hVectorMultY = CreateConVar("l4d_ghostpounce_vector_y", "900.0", "set how high is pounce", FCVAR_NOTIFY); 

	//AutoExecConfig(true, "l4d_ghostpounce");
}

public void OnConfigsExecuted()
{
	GetCvars();
}

public void cvarChanged(Handle convar, const char[] oldValue, const char[] newValue)
{
	GetCvars();
}

void GetCvars()
{
	g_iVectorMultX = g_hVectorMultX.IntValue;
	g_iVectorMultZ = g_hVectorMultZ.IntValue;
	g_fVectorMultY = g_hVectorMultY.FloatValue;
}

public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3], int &weapon)
{
	if(buttons & IN_RELOAD)
	{
		if(IsFakeClient(client) || GetClientTeam(client) != 3) 
			return Plugin_Continue;

		if(!IsPlayerSpawnGhost(client)) 
			return Plugin_Continue;

		if(GetEntityFlags(client) & FL_ONGROUND)
			DoPounce(client);
	}

	return Plugin_Continue;
}

void DoPounce(any client)
{
	float vVelocity[3];
	GetEntPropVector(client, Prop_Data, "m_vecVelocity", vVelocity);

	if(vVelocity[2] != 0)
		return;

	if(vVelocity[0] == 0 && vVelocity[1] == 0)
		return;

	vVelocity[0] *= g_iVectorMultX;
	vVelocity[1] *= g_iVectorMultZ;
	vVelocity[2] = g_fVectorMultY;

	TeleportEntity(client, NULL_VECTOR, NULL_VECTOR, vVelocity);
}

stock bool IsPlayerSpawnGhost(int client)
{
	return GetEntProp(client, Prop_Send, "m_isGhost") == 1;
}